<?php

include_once ("DatabaseManager.php");
include_once ("UserDirectory.php");
include_once ("ZipContainer.php");
include_once ("LatexTemplateEditor.php");
include_once ("DocxTemplateEditor.php");
include_once ("MetaDataToTemplate.php");

session_start();
$user_id = $_SESSION['user_id'];

$db = new DatabaseManager();
$con = $db->getConnection();

if (!$con)
 	die('Could not connect: ' . mysql_error());
 	
$db-> createDB();

$active_option = "start";
$action = "";

if(!empty($_GET['action'])){
	$action = $_GET['action'];

	switch($action)
	{
		case "user_choosen":
			$active_option = "categories";
			
			if($_POST['button'] == "continue" )
				$_SESSION['user_id'] = $_POST['user_id'];
			if($_POST['button'] == "edit" )
			{
				$_SESSION['user_id'] = $_POST['user_id'];
		  	$active_option = "start";
			}
			if($_POST['button'] == "new user" )
				$active_option = "start";
			if($_POST['button'] == "delete user")
			{
				$_SESSION['user_id'] = intval($_POST['user_id']);
				
		    $sql = "DELETE FROM `template_dragon`.`userdata` WHERE `userdata`.`user_id` = '".$_SESSION['user_id']."'";
		    $result2 = mysql_query($sql);
		    $active_option = "start";
			}		 
			break;
			
		case "category_choosen":
			$active_option = "templates";
			
			if($_POST['button'] == "continue" )
				$_SESSION['category_id'] = $_POST['category_id'];
			break;
			
		case "template_choosen":
			$active_option = "editing";
			
			if($_POST['button'] == "continue" )
			{
				$_SESSION['template_id'] = intval($_POST['file_id']);
				
				$sql 		= "SELECT template_name FROM files WHERE file_id = '".$_SESSION['template_id']."' LIMIT 1";
	    	$result = mysql_query($sql);
	    	$res 		= mysql_fetch_array($result);
	
	    	$template_name = $res['template_name'];
	    
				$template_dir_path 	= dirname(__FILE__)."/../users/".$_SESSION['user_id']."/templates/".$_SESSION['category_id']."/".$template_name."/";
				$user_dir_path 			= dirname(__FILE__)."/../users/".$_SESSION['user_id']."/home/";
				$user_dir 					= new UserDirectory();
				$user_dir->prepare($user_dir_path, $template_dir_path, $template_name);
				$_SESSION['user_dir'] = $user_dir;
				
				$_SESSION['meta_id'] = $_POST['meta_id'];
				
				if($_POST['meta_id']){
					$_SESSION['insert_metadata'] = new MetaDataToTemplate();
					$_SESSION['insert_metadata']->replace($_POST['meta_id'], $user_dir_path,$template_dir_path);
				}
				
				if($_SESSION['category_id']==1)
				{
				   $_SESSION['editor'] = new LatexTemplateEditor(); 
				} 
				else
				{
					$_SESSION['editor'] = new DocxTemplateEditor();
				} 
			    $_SESSION['editor']->init($user_dir->getPath());
			}
			else if($_POST['button'] == "upload template")
				$active_option = "templates";
				
			else if($_POST['button'] == "insert metadata")
				$active_option = "templates";
				
			else if($_POST['button'] == "delete metadata" )
			{
		  	$_SESSION['meta_id'] = intval($_POST['meta_id']);	  
				if($_SESSION['meta_id'])
				{
		    	  $sql = "DELETE FROM `template_dragon`.`metadata` WHERE `metadata`.`meta_id` = '".$_SESSION['meta_id']."'";
		    	  $result2 = mysql_query($sql);
				}				
		    $active_option = "templates";
			}
			else if ($_SESSION['user_dir'])
		    $_SESSION['user_dir']->refresh();
				    
			break;
		
		case "template_configured":
		  $latex_editor = $_SESSION["editor"];
			$latex_editor->replaceFields($_POST);
		    
			$active_option = "control";
			$zip_file = new ZipContainer();
			$zip_file->create($_SESSION['user_dir']->getPath(), $_SESSION['user_dir']->getTemplateName());
			$_SESSION['zip'] = $zip_file;
			break;
		
		case "template_checked":
			$active_option = "control";
			
			if($_POST['button'] == "Mail" )
				$active_option = "control";
				
			if($_POST['button'] == "Down" )
				$active_option = "control";
				
			break;
	}
}

html_start();

echo '<div id="status_bar">';
menu_bar($active_option);
echo "</div>";

echo "<div id='page'>";
switch ($action)
{
	case "user_choosen":
		$user_id = $_POST['user_id'];
		switch($_POST['button'])
		{
			case "edit":
				include ("ChangeUserData.php");
				break;
			case "new user":
				include ("userdata.html");
				break;
			case "delete user":
				include ("delusrsuc.php");
				break;
			default:
				choose_category();
				break;
		}
		break;
	
	case "category_choosen":
		//$category_id = $_POST['category_id'];
		//if($_POST['button'])
			choose_template();

		break;
	
	case "template_choosen":
		$template_id = $_POST['file_id'];
		switch($_POST['button'])
		{
		  case "upload template":
			  include("fileupload.html");
			  break;
		  case "insert metadata":
			 	include("metadata.html");
				break;
			case "delete metadata":
			  include("delmetasuc.php");
			 	break;
			default:
				config_template();
				break;
		}		
		break;
	
	case "template_configured":
		if($_POST['button'])
		  check_template();
		break;
	
	case "template_checked":
		switch($_POST['button'])
		{
			case "Mail":
				include ("maildata.html");
				break;
			case "Down":
				include ("download.php");
				break;
			default:
				check_template();
				break;
		}
	break;
	
	default:
		start();
	break;
}
echo "</div>";

echo "<div id='logo'>";
echo "<img src='../pics/template-dragon_logo.png' width='250' />";
echo "</div>";

html_end();

function start()
{
	$output = '
	<form name="user_chooser" method="post" action="?action=user_choosen">
	<table>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
		<tr>
			<td></td>
			<td>%s</td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	$sql = "SELECT user_id, firstname, lastname FROM userdata";
	$result = mysql_query($sql);
	
	$user_pulldown = '<select name="user_id" id="user_wahl">';
	while($row = mysql_fetch_row($result))
	{
		$user_pulldown .= '<option value="'.$row[0].
		'">'.$row[1]." ".$row[2].'</option>';
	}
	
	$user_pulldown .= '</select>';
	$next_button = '<input type="submit" name="button" id="continue" value="continue" />';
	$edit_button = '<input type="submit" name="button" id="edit" value="edit"/>';
	$new_user_button = '<input type="submit" name="button" id="new" value="new user"/>';
	$del_user_button = '<input type="submit" name="button" id="delusr" value="delete user"/>';
	
	printf($output, $user_pulldown, $new_user_button, $next_button, $edit_button, $del_user_button);
}

function choose_category()
{
	$output = '
	<form name="category_chooser" method="post" action="?action=category_choosen">
	<table>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	$sql = "SELECT category_id, category_name FROM categories";
	$result = mysql_query($sql);
	
	$category_pulldown = '<select name="category_id" id="category wahl">';
	
	while($row = mysql_fetch_row($result))
	{
		$category_pulldown .= '<option value='.$row[0].
		'>'.$row[1].'</option>';
	}
	
	$category_pulldown .= '</select>';
	$next_button = '<input type="submit" name="button" id="continue" value="continue" />';
	
	printf($output, $category_pulldown, $next_button);
}

function choose_template()
{
	$output = '
	<form name="template_chooser" method="post" action="?action=template_choosen">
	<table>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
		<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>' .
		'<tr>
			<td>%s</td>
			<td>%s</td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
	$cat_id = intval($_SESSION['category_id']);
	 
	$sql = "SELECT file_id, template_name FROM files WHERE category_id = '".$cat_id."'";
	$result = mysql_query($sql);
	
	$template_pulldown = '<select name="file_id" id="template wahl">';
	
	while($row = mysql_fetch_row($result))
	{
		$template_pulldown .= '<option value='.$row[0].
		'>'.$row[1].'</option>';
	}
	
	$template_pulldown .= '</select>';
	$next_button = '<input type="submit" name="button" id="continue" value="continue" />';
	$upload_button = '<input type="submit" name="button" id="upload" value="upload template" />';	
	$metaData_button = '<input type="submit" name="button" id="metaData" value="insert metadata" />';		
  $metaData_del_button = '<input type="submit" name="button" id="metaDataDel" value="delete metadata" />';	

	$user_id = intval($_SESSION['user_id']);
	
	$sql2 = "SELECT * FROM metadata WHERE user_id = '".$user_id."'";
	$result2 = mysql_query($sql2);
	
	$metaData_pulldown = '<select name="meta_id" id="MetaDataWahl">';
	
	while($row2 = mysql_fetch_row($result2))
	  $metaData_pulldown .= '<option value='.$row2[0].'>'.$row2[2].' '.$row2[3].' '.$row2[4].' '.$row2[5].' '.$row2[6].'</option>';
	
	$metaData_pulldown .= '</select>';

	printf($output, $template_pulldown, $upload_button,$metaData_pulldown,$metaData_button, $next_button, $metaData_del_button);
}

function config_template()
{
	$output = '
	<form name="template_configer" method="post" action="?action=template_configured">
	<table>
		%s
		<tr>
			<td></td>
			<td>%s</td>
		</tr>
	</table><p></p>
	</form>' . "\n";
	
    $cat_id = intval($_SESSION['category_id']);
//    if($cat_id == 1)
    {
      $latex_editor = $_SESSION["editor"];
	  $array_fields_to_replace = $latex_editor->getFieldsToEdit();
      $form = ""; 
      foreach($array_fields_to_replace as $entry)
     	{
     		$form .= '<tr><td>'.$entry["name"].':</td><td><input type="text" name="'.$entry["form_name"].'" value="'.$entry["value"].'"></td></tr>';
     	}
    }
	
	$next_button = '<input type="submit" name="button" id="weiter" value="continue" />';
	
	printf($output, $form, $next_button);
}

function check_template()
{
	$output = '

	<table>
		<tr>
			<td></td>
			<td></td>
		</tr>
		<tr>
			<td><form name="template_checker" method="post" action="?action=template_checked">%s</form></td>
			<td><form name="template_checker" method="post" action="download.php">%s</form></td>
		</tr>
	</table><p></p>
	' . "\n";
	
	if(!@$title = $_POST["title"])
		$title = 'NULL';
	if(!@$first_name = $_POST["first_name"])
		$first_name = 'NULL';
	if(!@$last_name = $_POST["last_name"])
		$last_name = 'NULL';
	
	$send_button = '<input type="submit" name="button" id="mail_send" value="Mail"/>';
	$down_button = '<input type="submit" name="button" id="zip_down" value="Down"/>';
	
	printf($output, $send_button, $down_button);
}

function menu_bar($active_option = "start")
{
	$menu_link1 = 'START';
	$menu_link2 = 'CATEGORIES';
	$menu_link3 = 'TEMPLATES';
	$menu_link4 = 'EDITING';
	$menu_link5 = 'CONTROL';
	
	switch($active_option)
	{
		case "start":
			$menu1 = "active_menu_color";
			$menu2 = "inactive_menu_color";
			$menu3 = "inactive_menu_color";
			$menu4 = "inactive_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = ' <a href="index.php" ><img src="../pics/start_active.png" width=150></a>';
			$menu_link2 = ' <img src="../pics/categories_inactive.png" width=150></a>';
			$menu_link3 = ' <img src="../pics/templates_inactive.png" width=150></a>';
			$menu_link4 = ' <img src="../pics/edit_inactive.png" width=150></a>';
			$menu_link5 = ' <img src="../pics/control_inactive.png" width=150></a>';
			break;
		case "categories":
			$menu1 = "visited_menu_color";
			$menu2 = "active_menu_color";
			$menu3 = "inactive_menu_color";
			$menu4 = "inactive_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.png" width=150></a>';
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/categories_active.png" width=150></a>';
			$menu_link3 = ' <img src="../pics/templates_inactive.png" width=150></a>';
			$menu_link4 = ' <img src="../pics/edit_inactive.png" width=150></a>';
			$menu_link5 = ' <img src="../pics/control_inactive.png" width=150></a>';
			break;
		case "templates":
			$menu1 = "visited_menu_color";
			$menu2 = "visited_menu_color";
			$menu3 = "active_menu_color";
			$menu4 = "inactive_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.png" width=150></a>';
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/categories_visited.png" width=150></a>';
			$menu_link3 = '<a href="index.php?action=category_choosen"><img src="../pics/templates_active.png" width=150></a>';
			$menu_link4 = ' <img src="../pics/edit_inactive.png" width=150></a>';
			$menu_link5 = ' <img src="../pics/control_inactive.png" width=150></a>';
			break;
		case "editing":
			$menu1 = "visited_menu_color";
			$menu2 = "visited_menu_color";
			$menu3 = "visited_menu_color";
			$menu4 = "active_menu_color";
			$menu5 = "inactive_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.png" width=150></a>';
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/categories_visited.png" width=150></a>';
			$menu_link3 = '<a href="index.php?action=category_choosen"><img src="../pics/templates_visited.png" width=150></a>';
			$menu_link4 = '<a href="index.php?action=template_choosen"><img src="../pics/edit_active.png" width=150></a>';
			$menu_link5 = ' <img src="../pics/control_inactive.png" width=150></a>';
			break;
		case "control":
			$menu1 = "visited_menu_color";
			$menu2 = "visited_menu_color";
			$menu3 = "visited_menu_color";
			$menu4 = "visited_menu_color";
			$menu5 = "active_menu_color";
			$menu_link1 = '<a href="index.php"><img src="../pics/start_visited.png" width=150></a>';
			$menu_link2 = '<a href="index.php?action=user_choosen"><img src="../pics/categories_visited.png" width=150></a>';
			$menu_link3 = '<a href="index.php?action=category_choosen"><img src="../pics/templates_visited.png" width=150></a>';
			$menu_link4 = '<a href="index.php?action=template_choosen"><img src="../pics/edit_visited.png" width=150></a>';
			$menu_link5 = '<a href="index.php?action=template_configured"><img src="../pics/control_active.png" width=150></a>';
			break;
	}
	
	$output = '
	<table class="menu">
		<tr>
			<td class ="menu_point"> <div class="' .$menu1 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/arrow.png" width=30></td>
			<td class ="menu_point"> <div class="' .$menu2 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/arrow.png" width=30></td>
			<td class ="menu_point"> <div class="' .$menu3 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/arrow.png" width=30></td>
			<td class ="menu_point"> <div class="' .$menu4 .'">%s<div></td>
			<td class ="menu_arrow"><img src="../pics/arrow.png" width=30></td>
			<td class ="menu_point"> <div class="' .$menu5 .'">%s<div></td>
		</tr>
	</table><p></p>' . "\n";
	
	printf($output, $menu_link1, $menu_link2, $menu_link3, $menu_link4, $menu_link5);
}

function html_start()
{
	echo '
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Template Dragon</title>
		<style type="text/css">@import "template-dragon_form-style.css";</style>
	</head>
	<body>';
}

function html_end()
{
	echo '
		</body>
	</html>';
}
?>